package secondary.proxy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import ontology.EmploymentLawOntology;
import ontology.UMCourtLOntology;
import ontology.UMCourtLVocabulary;
import ontology.VOOntology;
import ontology.actions.general.GetCase;
import ontology.actions.general.GetCases;
import ontology.actions.general.Index;
import ontology.actions.general.InformCase;
import ontology.actions.general.Log;
import ontology.actions.general.ParseCases;
import ontology.actions.general.ReturnList;
import ontology.actions.general.ReturnParseCases;
import ontology.concepts.employmentlaw.Case;
import ontology.concepts.employmentlaw.Norm;
import ontology.concepts.general.DBQueryRules;

import classes.AIDs;
import classes.MessageTemplates;
import classes.UMCourtAgent;
import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.lang.Codec;
import jade.content.lang.Codec.CodecException;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.onto.UngroundedException;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.util.leap.ArrayList;
import jade.util.leap.List;

public class Evaluator extends UMCourtAgent implements UMCourtLVocabulary {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 464937464163563007L;
	private Codec codec = new SLCodec();
	
	
	private Ontology ontology = EmploymentLawOntology.getInstance();
	
	
	private AIDs aids = new AIDs();
	
	
	@Override
	protected void setup() 
	{
		System.out.println(getDate()+ ": "+this.getLocalName()+" agent is now running...");
		registerAgent((String)getArguments()[0]);
		
		// Register language and ontology
	    getContentManager().registerLanguage(codec);
	
		System.out.println(getDate()+" : Connected to Database");
		
		this.addBehaviour(new receiveBehaviour());
	}
	
	@Override
	protected void takeDown()
	{
		System.out.println(getDate()+": "+this.getLocalName() +" is de-registering...");
		try { DFService.deregister(this); }
        catch (Exception e) {}
	}
	
	//behaviours
	class receiveBehaviour extends CyclicBehaviour
	{
		@Override
		public void action() 
		{
			ACLMessage msg = receive();
	        if (msg == null) 
	        { 
	        	block(); 
	        	return; 
	        }
	        
	        msg.removeReceiver((AID) msg.getAllIntendedReceiver().next());
	        
	        if(msg.getOntology().equals(EmploymentLawOntology.ONTOLOGY_NAME)){
	        	myprintln(msg.getContent());
	        	msg.addReceiver(aids.EVALUATORL);
	        	send(msg);
	        }
	        if(msg.getOntology().equals(VOOntology.ONTOLOGY_NAME)){
	        	msg.addReceiver(aids.EVALUATORVO);
	        	send(msg);
	        }
		}
	}
	
}
